package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.req.*;
import cn.cheegon.blog.entity.rsp.LoginRsp;
import cn.cheegon.blog.entity.rsp.UserInfoRsp;
import cn.cheegon.blog.model.UmsAdmin;
import cn.cheegon.blog.service.UmsAdminService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Desc:
 * @Author: cheegon
 * @Date: Create in 2024/7/1  上午9:55
 */
@RestController
@RequestMapping("/admin/*")
public class ClientController {
    @Autowired
    private UmsAdminService adminService;

    @PostMapping("login")
    public CommonResult<LoginRsp> login(@Validated @RequestBody LoginReq req) {
        return CommonResult.success(adminService.login(req.getUsername(), req.getPassword()));
    }

    @PostMapping(value = "register")
    public CommonResult register(@Validated @RequestBody RegisterReq req) {

        return CommonResult.success(adminService.register(req));
    }

    @GetMapping(value = "userInfo")
    public CommonResult<UserInfoRsp> userInfo() {
        UmsAdmin currUser = adminService.getCurrUser();
        return CommonResult.success(BeanUtil.toBean(currUser, UserInfoRsp.class));
    }

    @PostMapping(value = "editUserInfo")
    public CommonResult updateUserInfo(@Validated @RequestBody UserReq req) {
        UmsAdmin currUser = adminService.getCurrUser();
        req.setId(currUser.getId());
        adminService.update(BeanUtil.toBean(req, UmsAdmin.class));
        return CommonResult.success();
    }
    @PostMapping(value = "changePassword")
    public CommonResult changePassword(@Validated @RequestBody UpdatePasswordReq req) {
        UmsAdmin currUser = adminService.getCurrUser();
        adminService.updatePassword(currUser.getId(),req.getPassword(),req.getNewPassword());
        return CommonResult.success();
    }
    @PostMapping(value = "resetPassword")
    public CommonResult resetPassword(@Validated @RequestBody ResetPasswordReq req) {
        UmsAdmin currUser = adminService.getCurrUser();
        adminService.resetPassword(currUser.getId(),req.getPassword());
        return CommonResult.success();
    }

    @GetMapping("logout")
    public CommonResult logout() {
        return CommonResult.success(null);
    }
}
